package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.ArticleTable011Mapping;
import org.rcloud.io.model.Product;
import org.rcloud.io.model.ProductSite;
import org.rcloud.io.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/12.
 */
@RestController
@RequestMapping(value = "/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /*
     * 获取所有的农特产品信息
     * */
    @ApiOperation(value = "获取所有的农特产品信息", notes = "获取所有的农特产品信息")
    @ResponseBody
    @RequestMapping(value = "/findProduct", produces = {"application/json;charset=UTF-8"})
    public PageVo<Product> findProduct() {
        return productService.findProduct();
    }

    /*
  * 根据eid获取农特产品信息
  * */
    @ApiOperation(value = "根据eid获取农特产品信息", notes = "根据eid获取农特产品信息")
    @ResponseBody
    @RequestMapping(value = "/findByEid", produces = {"application/json;charset=UTF-8"})
    public ResultVo findByEid(@RequestBody String params) {
        JSONObject object = JSON.parseObject(params);
        return productService.findByEid(object.getString("eid"));
    }

    /*
    * 保存农特产品信息
    * */
    @ApiOperation(value = "保存农特产品信息", notes = "保存农特产品信息")
    @ResponseBody
    @RequestMapping(value = "/saveProduct", produces = {"application/json;charset=UTF-8"})
    public ResultVo saveProduct(@RequestBody String params) {

        JSONObject object = JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Product product = new Product();
        product.setEid(eid);
        product.setName(name);
        product.setAddress(address);
        product.setLongitude(longitude);
        product.setLatitude(latitude);
        product.setEtype(etype);
        product.setSummary(summary);
        product.setReport(report);
        product.setUsername(username);
        product.setUserphone(userphone);
        product.setFlag(1);

        return productService.saveproduct(product);
    }

    /*
    * 根据id获取农特产品信息
    * */
    @ApiOperation(value = "根据id获取农特产品信息", notes = "根据id获取农特产品信息")
    @ResponseBody
    @RequestMapping(value = "/findById", produces = {"application/json;charset=UTF-8"})
    public ResultVo findById(@RequestBody String params) {
        String[] ids = params.split("=");
        int id = Integer.parseInt(ids[1]);
        return productService.findById(id);
    }

    /*
     * 修改农特产品信息
     * */
    @ApiOperation(value="修改农特产品信息", notes="修改农特产品信息")
    @ResponseBody
    @RequestMapping(value = "/updateProduct", produces = {"application/json;charset=UTF-8"})
    public ResultVo updateProduct(@RequestBody String params) {
        JSONObject object = JSON.parseObject(params);
        Map map = JSON.parseObject(object.toString());

        int id = Integer.parseInt(map.get("id").toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Product product = new Product();
        product.setId(id);
        product.setEid(eid);
        product.setName(name);
        product.setAddress(address);
        product.setLongitude(longitude);
        product.setLatitude(latitude);
        product.setEtype(etype);
        product.setSummary(summary);
        product.setUsername(username);
        product.setUserphone(userphone);

        return productService.updateProduct(product);
    }

    /**
     * 查询淘山货大市所有的地点信息
     */
    @ApiOperation(value="查询淘山货大市所有的地点信息", notes="查询淘山货大市所有的地点信息")
    @ResponseBody
    @RequestMapping(value = "/findAllProductSite", produces = {"application/json;charset=UTF-8"})
    public PageVo<ProductSite> findAllProductSite(){
        return productService.findAllProductSite();
    }

    /**
     * 根据eid查询大市地点信息
     */
    @ApiOperation(value="根据eid查询大市地点信息", notes="根据eid查询大市地点信息")
    @ResponseBody
    @RequestMapping(value = "/findProductSiteByEid", produces = {"application/json;charset=UTF-8"})
    public ResultVo findProductSiteByEid(String eid){
        return productService.findProductSiteByEid(eid);
    }

    /**
     * 添加大市地点信息
     */
    @ApiOperation(value="添加大市地点信息", notes="添加大市地点信息")
    @ResponseBody
    @RequestMapping(value = "/saveProductSite", produces = {"application/json;charset=UTF-8"})
    public ResultVo saveProductSite(ProductSite productSite){
        return productService.insertProductSite(productSite);
    }

    /**
     * 修改大市地点信息
     */
    @ApiOperation(value="修改大市地点信息", notes="修改大市地点信息")
    @ResponseBody
    @RequestMapping(value = "/updateProductSite", produces = {"application/json;charset=UTF-8"})
    public ResultVo updateProductSite(ProductSite productSite){
        return productService.updateProductSite(productSite);
    }

    /**
     * 通过id查询大市地点
     */
    @ApiOperation(value="通过id查询大市地点", notes="通过id查询大市地点")
    @ResponseBody
    @RequestMapping(value = "/findProductSiteById", produces = {"application/json;charset=UTF-8"})
    public ResultVo findProductSiteById(Integer id){
        return productService.findProductSiteById(id);
    }

    @ApiOperation(value="淘山货大市地点绑定", notes="淘山货大市地点绑定")
    @ResponseBody
    @RequestMapping(value = "/binding", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo binding(Integer aId, Integer tId) {
        ArticleTable011Mapping articleTable011Mapping = new ArticleTable011Mapping();
        articleTable011Mapping.setaId(aId);
        articleTable011Mapping.settId(tId);
        return productService.savaMapping(articleTable011Mapping);
    }
}